package com.zy.explore.design.patterns.templatemethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 定义一个抽象的服务类，定义骨架流程（查询商户信息，加签，http请求，验签）
 *
 * @author ZY
 * @date 2021/11/02
 */
public abstract class AbstractMerchantService {

    /**
     * 模板方法流程
     *
     * @param request  请求
     * @param response 响应
     * @return {@link HttpServletResponse}
     */
    HttpServletResponse handleTemplate(HttpServletRequest request, HttpServletResponse response) {
        // 查询商户信息
        queryMerchantInfo(request, response);
        // 加签
        signature(request, response);
        // http请求
        httpRequest(request, response);
        // 验签
        verifySignature(request, response);
        return response;
    }

    /**
     * Http是否走代理（提供给子类实现）
     *
     * @return boolean
     */
    abstract boolean isRequestByProxy();


    /**
     * 查询商户信息
     *
     * @param request  请求
     * @param response 响应
     * @return {@link HttpServletResponse}
     */
    HttpServletResponse queryMerchantInfo(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("查询商户信息");
        return response;
    }

    /**
     * 加签
     *
     * @param request  请求
     * @param response 响应
     * @return {@link HttpServletResponse}
     */
    HttpServletResponse signature(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("加签");
        return response;
    }

    /**
     * http请求
     *
     * @param request  请求
     * @param response 响应
     * @return {@link HttpServletResponse}
     */
    HttpServletResponse httpRequest(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("http请求，是否使用代理：" + isRequestByProxy());
        return response;
    }

    /**
     * 验签
     *
     * @param request  请求
     * @param response 响应
     * @return {@link HttpServletResponse}
     */
    HttpServletResponse verifySignature(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("验签");
        return response;
    }

}
